﻿using System;
using System.Drawing;

namespace MslDiary.Framework
{
    public class ImageHelper
    {
        /// <summary>
        /// 生成缩略图
        /// </summary>
        /// <param name="originalPath">源图路径（物理路径带文件名）如：D:\IMG_0644.JPG</param>
        /// <param name="thumbnailPath">缩略图路径（物理路径不带文件名）如：D:\</param>
        /// <param name="isDateReName">是否以日期重命名</param>
        /// <param name="prefix">保存后缀，如：jpg</param>
        /// <param name="maxWidth">图片最大宽度</param>
        public static bool ChangeImage(string originalPath, string thumbnailPath, bool isDateReName, string prefix, int maxWidth)
        {
            if ((prefix + "").Length == 0)
            {
                prefix = System.IO.Path.GetExtension(originalPath).ToLower();
            }
            prefix = prefix.StartsWith(".") ? prefix : "." + prefix;
            string fileName = "";
            string strFileName = DateTime.Now.ToString("yyyyMMddHHmmss") + new Random().Next(1000) + prefix;

            try
            {
                if (!isDateReName)
                {
                    fileName = thumbnailPath + originalPath.Substring(originalPath.LastIndexOf("\\"));
                    fileName = fileName.Substring(0, fileName.LastIndexOf(".")) + prefix;
                }
                else
                {
                    fileName = thumbnailPath + strFileName;
                }
                using (System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalPath))
                {
                    #region ##

                    int StartW = 0;//开始宽坐标
                    int StartH = 0;//开始高坐标

                    int x = StartW;
                    int y = StartH;
                    int ow = originalImage.Width;
                    int oh = originalImage.Height;

                    int towidth = ow;
                    int toheight = oh;

                    if (maxWidth != 0)//
                    {
                        towidth = maxWidth;
                        toheight = originalImage.Height * towidth / originalImage.Width;
                    }

                    #endregion

                    using (System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight))
                    {
                        //新建画板
                        using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap))
                        {
                            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;//设置高质量插值法
                            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//设置高质量，低速度呈现平滑程度
                            g.Clear(System.Drawing.Color.Transparent);//清空画布并以透明背景色填充
                            g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight), //在指定位置并且按指定大小绘制原图片的指定部分
                                new System.Drawing.Rectangle(x, y, ow, oh), System.Drawing.GraphicsUnit.Pixel);
                        }
                        if (!prefix.StartsWith(".")) prefix = "." + prefix;

                        switch (prefix.ToLower())
                        {
                            case ".jpg":
                            case ".jpeg":
                            case ".pjpeg": bitmap.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg); break;
                            case ".gif": bitmap.Save(fileName, System.Drawing.Imaging.ImageFormat.Gif); break;
                            case ".bmp": bitmap.Save(fileName, System.Drawing.Imaging.ImageFormat.Bmp); break;
                            case ".png": bitmap.Save(fileName, System.Drawing.Imaging.ImageFormat.Png); break;
                            default: bitmap.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg); break;
                        }
                        return true;
                    }
                }
            }
            catch (System.Exception e)
            {
                MSL.Tool.LogHelper.Instance.WriteLog(e.ToString());
                return false;
            }
        }
    }
}
